#include "../../include/Geometry/Mesh.h"

using namespace z_mesh;

Mesh::Mesh()
{
}

Mesh::Mesh(const Mesh& mesh)
{
	this->indices = mesh.indices;
	this->vertices = mesh.vertices;
}

Mesh::~Mesh()
{
}

void Mesh::setVertices(z_vertex::Vertex* vertices, unsigned int count)
{
	this->vertices.resize(static_cast<unsigned long>(count));
	new(&this->vertices[0])std::vector<z_vertex::Vertex>(vertices, vertices + count);
}

void Mesh::setIndices(unsigned int* indices, unsigned int count)
{
	this->indices.resize(static_cast<unsigned long>(count));
	new(&this->indices[0])std::vector<unsigned int>(indices, indices + count);
}

Mesh Mesh::triangle(z_vector::Vector3f& p1, z_vector::Vector3f& p2, z_vector::Vector3f& p3)
{
	Mesh triangle;
	z_vertex::Vertex v1
		= z_vertex::Vertex
		(z_vector::Vector4f(1.0, 0.0, 0.0, 1.0),
			z_vector::Vector4f(p1, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v2
		= z_vertex::Vertex
		(z_vector::Vector4f(0.0, 1.0, 0.0, 1.0),
			z_vector::Vector4f(p2, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v3
		= z_vertex::Vertex
		(z_vector::Vector4f(0.0, 0.0, 1.0, 1.0),
			z_vector::Vector4f(p3, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));

	triangle.vertices.clear();
	triangle.vertices.push_back(v1);
	triangle.vertices.push_back(v2);
	triangle.vertices.push_back(v3);

	triangle.indices.clear();
	triangle.indices.push_back(0);
	triangle.indices.push_back(1);
	triangle.indices.push_back(2);


	return triangle;
}

Mesh Mesh::cube(double height_y, double width_z, double length_x)
{
	Mesh cube;
	z_vertex::Vertex v1
		= z_vertex::Vertex
		(z_vector::Vector4f(1.0, 1.0, 1.0, 1.0),
			z_vector::Vector4f(0.5, 0.5, 0.5, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v2
		= z_vertex::Vertex
		(z_vector::Vector4f(1.0, 0.0, 0.0, 1.0),
			z_vector::Vector4f(0.5, 0.5, -0.5, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v3
		= z_vertex::Vertex
		(z_vector::Vector4f(0.0, 1.0, 0.0, 1.0),
			z_vector::Vector4f(-0.5, 0.5, -0.5, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v4
		= z_vertex::Vertex
		(z_vector::Vector4f(0.0, 0.0, 1.0, 1.0),
			z_vector::Vector4f(-0.5, 0.5, 0.5, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));


	z_vertex::Vertex v5
		= z_vertex::Vertex
		(z_vector::Vector4f(1.0, 1.0, 1.0, 1.0),
			z_vector::Vector4f(0.5, -0.5, 0.5, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v6
		= z_vertex::Vertex
		(z_vector::Vector4f(1.0, 0.0, 0.0, 1.0),
			z_vector::Vector4f(0.5, -0.5, -0.5, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v7
		= z_vertex::Vertex
		(z_vector::Vector4f(0.0, 1.0, 0.0, 1.0),
			z_vector::Vector4f(-0.5, -0.5, -0.5, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v8
		= z_vertex::Vertex
		(z_vector::Vector4f(0.0, 0.0, 1.0, 1.0),
			z_vector::Vector4f(-0.5, -0.5, 0.5, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));


	cube.vertices.clear();
	cube.vertices.push_back(v1);
	cube.vertices.push_back(v2);
	cube.vertices.push_back(v3);
	cube.vertices.push_back(v4);
	cube.vertices.push_back(v5);
	cube.vertices.push_back(v6);
	cube.vertices.push_back(v7);
	cube.vertices.push_back(v8);


	cube.indices.clear();

	cube.indices.push_back(0);
	cube.indices.push_back(4);
	cube.indices.push_back(7);

	cube.indices.push_back(0);
	cube.indices.push_back(3);
	cube.indices.push_back(7);
	

	cube.indices.push_back(0);
	cube.indices.push_back(3);
	cube.indices.push_back(2);

	cube.indices.push_back(0);
	cube.indices.push_back(1);
	cube.indices.push_back(2);


	cube.indices.push_back(1);
	cube.indices.push_back(2);
	cube.indices.push_back(6);

	cube.indices.push_back(1);
	cube.indices.push_back(5);
	cube.indices.push_back(6);




	cube.indices.push_back(0);
	cube.indices.push_back(1);
	cube.indices.push_back(5);

	cube.indices.push_back(0);
	cube.indices.push_back(4);
	cube.indices.push_back(5);


	cube.indices.push_back(3);
	cube.indices.push_back(7);
	cube.indices.push_back(6);

	cube.indices.push_back(3);
	cube.indices.push_back(2);
	cube.indices.push_back(6);

	cube.indices.push_back(4);
	cube.indices.push_back(5);
	cube.indices.push_back(6);

	cube.indices.push_back(4);
	cube.indices.push_back(7);
	cube.indices.push_back(6);
	
	return cube;
}

Mesh Mesh::plane(double width, double length)
{
	return Mesh();
}


Mesh Mesh::tetrahedron()
{

	Mesh tetrahedron;
	z_vertex::Vertex v1
		= z_vertex::Vertex
		(z_vector::Vector4f(1.0, 1.0, 1.0, 1.0),
			z_vector::Vector4f(0, 1, 0, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v2
		= z_vertex::Vertex
		(z_vector::Vector4f(1.0, 0.0, 0.0, 1.0),
			z_vector::Vector4f(0, 0, 1, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v3
		= z_vertex::Vertex
		(z_vector::Vector4f(0.0, 1.0, 0.0, 1.0),
			z_vector::Vector4f(1, 0, 0, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));
	z_vertex::Vertex v4
		= z_vertex::Vertex
		(z_vector::Vector4f(0.0, 0.0, 1.0, 1.0),
			z_vector::Vector4f(0, 0, -1, 1.0),
			z_vector::Vector3f(0, 0, 0),
			z_vector::Vector2f(0, 0));



	tetrahedron.vertices.clear();
	tetrahedron.vertices.push_back(v1);
	tetrahedron.vertices.push_back(v2);
	tetrahedron.vertices.push_back(v3);
	tetrahedron.vertices.push_back(v4);

	tetrahedron.indices.clear();
	tetrahedron.indices.push_back(0);
	tetrahedron.indices.push_back(1);
	tetrahedron.indices.push_back(2);

	tetrahedron.indices.push_back(0);
	tetrahedron.indices.push_back(1);
	tetrahedron.indices.push_back(3);

	tetrahedron.indices.push_back(0);
	tetrahedron.indices.push_back(2);
	tetrahedron.indices.push_back(3);

	tetrahedron.indices.push_back(1);
	tetrahedron.indices.push_back(2);
	tetrahedron.indices.push_back(3);

	return tetrahedron;
}